package com.dbh.alg.algorithm.hot100.哈希;

import java.util.HashSet;
import java.util.Set;

/**
 * @author 董帮辉
 * @date 2025/7/13
 **/
public class Leetcode128_最长连续序列 {

    public int longestConsecutive(int[] nums) {
        Set<Integer> numSet = new HashSet<>();
        for (int num : nums) {
            numSet.add(num);
        }
        int longestStreak = 0;
        for (Integer num : numSet) {
            // 找到连续最大的值
            if (!numSet.contains(num + 1)) {
                int currentNum = num;
                int currentStreak = 1;
                while (numSet.contains(currentNum - 1)) {
                   currentNum--;
                   currentStreak++;
                }
                longestStreak = Math.max(longestStreak, currentStreak);
            }
        }
        return longestStreak;
    }
}
